package org.dey.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.dey.mapper.LibraryMapper;
import org.dey.pojo.Dietary;
import org.dey.pojo.Library;
import org.dey.pojo.PageVo;
import org.dey.service.LibraryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class LibraryServiceimpl extends ServiceImpl<LibraryMapper, Library> implements LibraryService {
    @Autowired(required = false)
    LibraryMapper libraryMapper;

    @Override
    public PageVo<Library> queryLibrary(Library library,int pageNum, int pageSize) {
       //组装条件
        QueryWrapper<Library> queryWrapper = new QueryWrapper<Library>();

        //根据名字模糊查询
        if(StringUtils.isNotBlank(library.getDisease())) {
            queryWrapper.like("disease", library.getDisease());
        }

        PageHelper.startPage(pageNum, pageSize);
        List<Library> libraryList = this.list(queryWrapper);

        PageInfo<Library> pageInfo = new PageInfo<Library>(libraryList);
        PageVo<Library> pageVo =new PageVo<Library>();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());
        return pageVo;
    }

    @Override
    public int insertLibrary(Library library) {
        int num = 0;

        QueryWrapper<Library> queryWrapper = new QueryWrapper<Library>();
        queryWrapper.eq("disease", library.getDisease());

        Library one = this.getOne(queryWrapper);

        if(one==null){
            boolean save = this.save(library);
            if(save){
                num=1;
            }
        }
        return num;
    }

    @Override
    public int deleteLibrary(int id) {
        int num = 0;
        boolean b = this.removeById(id);
        if(b){
            num = 1;
        }
        return num;
    }

    @Override
    public Library queryLibraryByid(int id) {
        return this.getById(id);
    }

    @Override
    public int updateLibrary(Library library) {
        int num = 0;
        boolean b = this.updateById(library);
        if(b){
            num = 1;
        }
        return num;
    }
}
